package com.daluzy.chmanager.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.daluzy.chmanager.MyApplication;
import com.daluzy.chmanager.R;
import com.daluzy.chmanager.bean.Area;
import com.daluzy.chmanager.bean.Record;
import com.daluzy.chmanager.bean.School;
import com.daluzy.chmanager.bean.TestPoint;
import com.xiasuhuei321.loadingdialog.view.LoadingDialog;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

/**
 * 添加管理员
 */
public class AddRecordActivity extends AppCompatActivity {


    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_record_test_point)
    TextView tvRecordTestPoint;
    @BindView(R.id.tv_record_area)
    TextView tvRecordArea;
    @BindView(R.id.et_record_title)
    EditText etRecordTitle;
    @BindView(R.id.et_record_details)
    EditText etRecordDetails;
    @BindView(R.id.et_record_remark)
    EditText etRecordRemark;
    @BindView(R.id.tv_add_commit)
    TextView tvAddCommit;

    private String recordTitle;
    private String recordDetails;
    private String recordDRemark;

    private LoadingDialog loadingDialog;
    private TestPoint testPoint;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_record_daluzy);
        ButterKnife.bind(this);
        testPoint = (TestPoint) getIntent().getSerializableExtra("testPoint");

        tvRecordArea.setText(testPoint != null ? testPoint.getArea_name() : null);
        tvRecordTestPoint.setText(testPoint.getPoint_name());
        loadingDialog = new LoadingDialog(this);
    }

    @OnClick({R.id.iv_back,  R.id.tv_add_commit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_add_commit:
                tvAddCommit.setEnabled(false);
                check();
                break;
        }
    }

    private void check() {
         recordTitle = etRecordTitle.getText().toString().trim();
         recordDetails = etRecordDetails.getText().toString().trim();;
         recordDRemark = etRecordRemark.getText().toString().trim();;

        if (TextUtils.isEmpty(recordTitle)) {
            Toast.makeText(AddRecordActivity.this, "请输入保修标题", Toast.LENGTH_SHORT).show();
            tvAddCommit.setEnabled(true);
            return;
        }
        if (TextUtils.isEmpty(recordDetails)) {
            Toast.makeText(AddRecordActivity.this, "请输入保修内容", Toast.LENGTH_SHORT).show();
            tvAddCommit.setEnabled(true);
            return;
        }

        addRecord();
    }


    private void addRecord() {
        Record record = new Record();
        record.setRecord_id(String.valueOf(System.currentTimeMillis()));
        record.setRecord_area(testPoint.getArea_name());
        record.setRecord_test_point(testPoint.getPoint_name());
        record.setRecord_student(MyApplication.getIntance().getStudents().getStudent_name());
        record.setRecord_details(recordDetails);
        record.setRecord_title(recordTitle);
        record.setRecord_remark(recordDRemark);
        record.setRecord_type("1");
        record.save(new SaveListener<String>() {
            @Override
            public void done(String objectId, BmobException e) {
                if (e == null) {
                    Toast.makeText(AddRecordActivity.this, "添加成功", Toast.LENGTH_SHORT).show();
                    finish();
                } else {
                    Toast.makeText(AddRecordActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
                }
                if (loadingDialog != null)
                    loadingDialog.close();
                tvAddCommit.setEnabled(true);
            }
        });
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

    }
}
